<?php

namespace App\Store\Controllers\Goods;

use App\Common\Services\Store\Common as StoreCommon;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;


use App\Common\Services\CommonExcelExport;
use Maatwebsite\Excel\Facades\Excel;
use App\Store\Models\Goods\GoodsLog;

/**
 * 商品流水列表
 */
class GoodsLogController extends Controller
{
    /**
     * 商品流水列表
     */
    public function getlist(Request $request)
    {

        $list = GoodsLog::getList($request);
        return response()->json(['code' => '000', 'msg' => '获取成功', 'data' => $list]);
    }

    /**
     * 导出
     */
    public function doexport(Request $request)
    {

        $accountinfo = StoreCommon::getCurrentAccountinfo($request);
        $type = $accountinfo['type'] ?? 0;
        //1入库，2加库存，3减库存，4编辑;调拨：11调拨入库，12调拨出库，13取消调拨;返厂：21返厂出库，22取消返厂，23返厂入库;销售相关：91销售出库，92销售取消，93退货上柜，94取消上柜
        $logtype = [
            1 => '入库',
            2 => '加库存',
            3 => '减库存',
            4 => '编辑',
            5 => '撤审',
            11 => '调拨入库',
            12 => '调拨出库',
            13 => '取消调拨',
            21 => '返厂出库',
            22 => '取消返厂',
            23 => '返厂入库',
            91 => '销售出库',
            92 => '销售取消',
            93 => '退货上柜',
            94 => '取消上柜',
            99 => '商品删除'
        ];
        $list = GoodsLog::getList($request)['list'];


        if($type>0&& in_array($type,[1,2])){
            $row = [
                ['table_header' => '商品流水日志'],
                [
                    'startstore' => '起始门店',
                    'targetstore' => '目标门店',
                    'goods_sn_code' => '条码',
                    'goods_name' => '商品名称',
                    'goods_category_name' => '商品类别',
                    'cf_no' => '证书号',
                    'count' => '数量',
                    'piece_weight' => '重量',
                    'netglod_weight' => '金重',
                    'label_price' => '标价',
                    'log_type' => '类型',
                    'with_sn_code' => '关联单号',
                    'create_account_real_name' => '操作人',
                    'is_code_more' => '一码多货',
                    'stock_at' => '库存时间',
                    'create_at' => '操作时间',
                ]];
        }else{
            $row = [
                ['table_header' => '商品流水日志'],
                [
                    'startstore' => '起始门店',
                    'targetstore' => '目标门店',
                    'goods_sn_code' => '条码',
                    'goods_name' => '商品名称',
                    'goods_category_name' => '商品类别',
                    'cf_no' => '证书号',
                    'count' => '数量',
                    'piece_weight' => '重量',
                    'netglod_weight' => '金重',
                    'label_price' => '标价',
                    'piececost_fee' => '单件成本',
                    'total_cost_fee' => '总成本',
                    'log_type' => '类型',
                    'with_sn_code' => '关联单号',
                    'create_account_real_name' => '操作人',
                    'is_code_more' => '一码多货',
                    'stock_at' => '库存时间',
                    'create_at' => '操作时间',
                ]
            ];
        }


        $exceldata = [];
        foreach ($list as $val) {
            $exceldata[] = [
                'startstore' => isset($val['start_store_info']) ? $val['start_store_info']['store_name'] : '财务部',
                'targetstore' => isset($val['target_store_info']['store_name']) ? $val['target_store_info']['store_name'] : '财务部',
                'goods_sn_code' => $val['goods_sn_code'],
                'goods_name' => $val['goods_name'],
                'goods_category_name' => $val['goods_category']['category_name'] ?? '',
                'cf_no' => $val['cf_no'],
                'count' => $val['count'],
                'piece_weight' => $val['piece_weight'],
                'netglod_weight' => $val['netglod_weight'],
                'label_price' => $val['label_price'] / 100,
                'piececost_fee' => $val['piececost_fee'] / 100,
                'total_cost_fee' => $val['total_cost_fee'] > 0 ? ($val['total_cost_fee'] / 100) : 0,
                'log_type' => isset($logtype[$val['log_type']]) ? $logtype[$val['log_type']] : '',
                'with_sn_code' => $val['with_sn_code'],
                'create_account_real_name' => $val['create_account_real_name'],
                'is_code_more' => $val['is_code_more'] == 1 ? '否' : '是',
                'stockin_at' => date('Y-m-d H:i:s', $val['stockin_at']),
                'create_at' => date('Y-m-d H:i:s', $val['create_at']),
            ];
        }
        $filename = '商品流水信息导出_' . time() . '.xlsx';
        return Excel::download(new CommonExcelExport($row, $exceldata), $filename);


    }

}
